package com.whfc.ms.api.fmam.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fmam.entity.dto.FmamWeighNoteCountDTO;
import com.whfc.fmam.entity.dto.FmamWeighNoteDTO;
import com.whfc.fmam.entity.dto.FmamWeighNoteDailyDTO;
import com.whfc.fmam.entity.dto.FmamWeighNoteDetailDTO;
import com.whfc.fmam.entity.qo.WeighNoteListQO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fmam.service.WeighNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;

/**
 * @author sun_guodong
 * @description 磅单接口
 * @date 2019-11-25
 */
@RestController
@RequestMapping("/ms/api/fmam/weighNote")
public class FmamWeighNoteController extends BaseController {
    @Autowired
    private WeighNoteService weighNoteService;

    @RequestMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated WeighNoteListQO request) {
        logger.info("收发料明细台账列表，loginer:{},WeighNoteListQO:{}", user.getUsername(), request.toString());
        PageData<FmamWeighNoteDTO> pageData = weighNoteService.list(request);
        return ResultUtil.success(pageData);
    }


    @GetMapping("/detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("id") Long id) {
        logger.info("查询磅单详情，loginer:{},id:{}", user.getUsername(), id);
        FmamWeighNoteDetailDTO weighNoteDetailDTO = weighNoteService.detail(id);
        return ResultUtil.success(weighNoteDetailDTO);
    }


    @GetMapping(value = {"/anon/info/{guid}", "/info/{guid}"})
    public Result info(@PathVariable("guid") String guid) {
        logger.info("二维码获取磅单信息，guid:{}", guid);
        FmamWeighNoteDetailDTO weighNoteDetailDTO = weighNoteService.info(guid);
        return ResultUtil.success(weighNoteDetailDTO);
    }


    @PostMapping("/export")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestBody @Validated WeighNoteListQO request) throws IOException {
        logger.info("导出收发料明细台账列表，loginer:{},param:{}", user.getUsername(), request.toString());
        OssPathDTO data = weighNoteService.export(request);
        return ResultUtil.success(data);
    }

    @GetMapping("/log")
    public Result log(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                      @RequestParam("deptId") Integer deptId) {
        logger.info("物资日志,loginer:{},date:{},deptId:{}", user.getUsername(), date, deptId);
        FmamWeighNoteCountDTO data = weighNoteService.weighNoteCount(deptId, date);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/daily")
    public Result daily(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                        @RequestParam("deptId") Integer deptId,
                        @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd") Date time,
                        @RequestParam(value = "weighNoteType") Integer weighNoteType,
                        @RequestParam(value = "pageNum") Integer pageNum,
                        @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("材料进出场-日报,loginer:{},deptId:{},time:{},weighNoteType:{},pageNum:{},pageSize:{}", user.getUsername(), deptId, time, weighNoteType, pageNum, pageSize);
        PageData<FmamWeighNoteDailyDTO> pageData = weighNoteService.daily(deptId, time, weighNoteType, pageSize, pageNum);
        return ResultUtil.success(pageData);
    }

}
